package com.ruoyi.consumer.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.consumer.domain.CDealCompany;
import com.ruoyi.consumer.domain.CUser;
import com.ruoyi.consumer.domain.param.DealPersonDTO;
import com.ruoyi.consumer.service.impl.CUserServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.consumer.domain.CDealPerson;
import com.ruoyi.consumer.service.ICDealPersonService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 个人钱包流水记录Controller
 * 
 * @author ruoyi
 * @date 2023-11-17
 */
@RestController
@RequestMapping("/consumer/dealPerson")
public class CDealPersonController extends BaseController
{
    @Autowired
    private ICDealPersonService cDealPersonService;
    @Autowired
    private CUserServiceImpl userService;

    public TableDataInfo getDataTable(List<CDealPerson> list, List<?> list1)
    {
        // 使用原始 list 创建 PageInfo 对象来保留分页信息
        PageInfo<CDealPerson> pageInfo = new PageInfo<>(list);
        TableDataInfo rspData = new TableDataInfo(list1, (int)pageInfo.getTotal());
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list1);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }
    /**
     * 查询个人钱包流水记录列表
     */
    @PreAuthorize("@ss.hasPermi('consumer:dealPerson:list')")
    @GetMapping("/list")
    public TableDataInfo list(CDealPerson cDealPerson)
    {
        startPage();
        List<CDealPerson> list = cDealPersonService.selectCDealPersonList(cDealPerson);
        startPage();
        List<DealPersonDTO> ajaxResults = new ArrayList<>();
        for (CDealPerson dealPerson : list) {
            CUser user = userService.selectCUserByUserId(dealPerson.getUserId());
            DealPersonDTO dealPersonDTO = new DealPersonDTO(dealPerson,user);
            ajaxResults.add(dealPersonDTO);
        }
        return getDataTable(list,ajaxResults);
    }

    /**
     * 导出个人钱包流水记录列表
     */
    @PreAuthorize("@ss.hasPermi('consumer:dealPerson:export')")
    @Log(title = "个人钱包流水记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CDealPerson cDealPerson)
    {
        List<CDealPerson> list = cDealPersonService.selectCDealPersonList(cDealPerson);
        ExcelUtil<CDealPerson> util = new ExcelUtil<CDealPerson>(CDealPerson.class);
        util.exportExcel(response, list, "个人钱包流水记录数据");
    }

    /**
     * 获取个人钱包流水记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('consumer:dealPerson:query')")
    @GetMapping(value = "/{dealRecordId}")
    public AjaxResult getInfo(@PathVariable("dealRecordId") Integer dealRecordId)
    {
        return success(cDealPersonService.selectCDealPersonByDealRecordId(dealRecordId));
    }

    /**
     * 新增个人钱包流水记录
     */
    @PreAuthorize("@ss.hasPermi('consumer:dealPerson:add')")
    @Log(title = "个人钱包流水记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CDealPerson cDealPerson)
    {
        return toAjax(cDealPersonService.insertCDealPerson(cDealPerson));
    }

    /**
     * 修改个人钱包流水记录
     */
    @PreAuthorize("@ss.hasPermi('consumer:dealPerson:edit')")
    @Log(title = "个人钱包流水记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CDealPerson cDealPerson)
    {
        return toAjax(cDealPersonService.updateCDealPerson(cDealPerson));
    }

    /**
     * 删除个人钱包流水记录
     */
    @PreAuthorize("@ss.hasPermi('consumer:dealPerson:remove')")
    @Log(title = "个人钱包流水记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dealRecordIds}")
    public AjaxResult remove(@PathVariable Integer[] dealRecordIds)
    {
        return toAjax(cDealPersonService.deleteCDealPersonByDealRecordIds(dealRecordIds));
    }
}
